
package com.be.beadmin.device.impl;

import com.be.beadmin.device.entity.InspectStandardCategory;
import com.be.beadmin.device.entity.InspectStandardCategory_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.InspectStandardCategoryMapperEx;
import com.be.beadmin.device.repository.InspectStandardCategoryRepository;
import com.be.beadmin.device.service.InspectStandardCategoryService;
import com.be.beadmin.device.dto.input.InspectStandardCategoryDto;
import com.be.beadmin.device.dto.output.InspectStandardCategoryEx;
import com.be.beadmin.device.dto.input.InspectStandardCategoryQueryCriteria;
import com.be.beadmin.device.mapper.InspectStandardCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.*;

/**
* @description 服务实现
* @author zhouzhou
* @date 2023-09-19
**/
@Service
@RequiredArgsConstructor
public class InspectStandardCategoryServiceImpl implements InspectStandardCategoryService {

    @Autowired
    private  InspectStandardCategoryRepository inspectStandardCategoryRepository;
    @Autowired
    private  InspectStandardCategoryMapper inspectStandardCategoryMapper;
    @Autowired
    private  InspectStandardCategoryMapperEx inspectStandardCategoryExMapper;

    @Override
    public Page<InspectStandardCategoryEx> getInspectStandardCategoryByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<InspectStandardCategory> page = inspectStandardCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(InspectStandardCategory_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(InspectStandardCategory_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<InspectStandardCategoryEx> page1 = page.map(inspectStandardCategoryExMapper::toEntity);
            return page1;

    }

    @Override
    public void addInspectStandardCategory(InspectStandardCategoryDto inspectStandardCategoryDto) {
            InspectStandardCategory result = inspectStandardCategoryRepository.findInspectStandardCategoryByIdAndDelete(inspectStandardCategoryDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("InspectStandardCategory不存在,请重新查询");
            }
            InspectStandardCategory inspectStandardCategory = inspectStandardCategoryMapper.toEntity(inspectStandardCategoryDto);
            inspectStandardCategoryRepository.save(inspectStandardCategory);
    }

    @Override
    public void updateInspectStandardCategory(InspectStandardCategoryDto inspectStandardCategoryDto) {
            InspectStandardCategory inspectStandardCategory = inspectStandardCategoryRepository.findInspectStandardCategoryById(inspectStandardCategoryDto.getId());
            inspectStandardCategory = inspectStandardCategoryMapper.toEntity(inspectStandardCategoryDto);
            inspectStandardCategoryRepository.save(inspectStandardCategory);
    }


    @Override
    public void delInspectStandardCategory(String id) {
        InspectStandardCategory inspectStandardCategory = inspectStandardCategoryRepository.findInspectStandardCategoryById(id);
        inspectStandardCategory.setDelete(1);
        inspectStandardCategoryRepository.save(inspectStandardCategory);
    }

    @Override
    public void delInspectStandardCategorys (List<String> ids) { }

    @Override
    public List<String> getTemplateId() {
        List<String> productTypeList  = inspectStandardCategoryRepository.findTemplateIds();
        return productTypeList;
    }

    @Override
    public Map<String, String> getTemplateIdAndNames() {
        List<String> templateIds = getTemplateId();
        Map<String, String> map = new HashMap<String, String>();
        for(String id : templateIds){
            String templateNameById = inspectStandardCategoryRepository.findTemplateNameById(id);
            map.put(id, templateNameById);
        }
        return map;
    }


}